package com.cyc.basic.leecode.查找;

import java.lang.reflect.InvocationTargetException;

/**
 * @author chenyunchang
 * @title
 * @date 2021/3/17 11:28
 * @Description: 数组中重复的数字
 */
public class LeecodeTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //原始数据
        int[] str = {2, 3, 1, 0, 2, 5, 3};

        Solution1 solution = new Solution1();
        //校验数组中的重复数字
        int repeatNumber = solution.findRepeatNumber(str);
        System.out.println(repeatNumber);
    }
}


class Solution1 {
    public int findRepeatNumber(int[] nums) {
        for (int i = 0, n = nums.length; i < n; ++i) {
            while (nums[i] != i) {
                if (nums[i] == nums[nums[i]]) {
                    return nums[i];
                }
                swap(nums, i, nums[i]);
            }
        }
        return -1;
    }

    private void swap(int[] nums, int i, int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
}
